<?php
namespace app\client\controller;

class Device extends Base {
    
    public function __construct() {
        parent::__construct();
    }

    public function checkDevice() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $deviceNo = request()->get('deviceNo', 0, 'intval');
        if($deviceNo <= 0) {
            return errorResult();
        }
        
        $device = model('Device')->findOne(['device_no' => $deviceNo], ['status', 'heart_time']);
        if(empty($device) || $device['lock_status'] == 1){
            return errorResult('设备暂不可用');
        }
        
        if(($device['heart_time'] + 300) < time()){
            return errorResult('设备已掉线');
        }
        
        return successResult();
    }
    
    public function getDevice() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $deviceNo = request()->get('deviceNo', 0, 'intval');
        if($deviceNo <= 0) {
            return errorResult();
        }
        
        $device = model('Device')->findOne(['device_no' => $deviceNo], ['device_id', 'signal', 'agent_id', 'lock_status', 'heart_time', 'device_type', 'charge_mode', 'price_id', 'address', 'longitude', 'latitude']);
        if(empty($device) || empty($device['price_id'])){
            return errorResult('设备不存在或者未使用2');
        }
        
        $device['online'] = true;
        if($device['lock_status'] == 1 || ($device['heart_time'] + 300) <= time()){
            $device['online'] = false;
        }
        
        if($device['online'] == false) {
            $device['signal'] = 0;
        }
        
        //客服电话
        if(!empty($device['agent_id'])){
            $device['service_telephone'] = model('Agent')->where(['agent_id' => $device['agent_id']])->value('service_telephone');
        }else{
            $device['service_telephone'] = model('Platform')->value('service_telephone');
        }
        
        $repData['device'] = $device;
        
        $repData['port'] = model('DevicePort')->findAll(['device_id' => $device['device_id']], ['status', 'port_no', 'port_id']);
        
        $chargePrice = model('ChargePrice')->findOne(['price_id' => $device['price_id']], ['options', 'charge_type']);
        if(empty($chargePrice)) {
            return errorResult('设备不存在或者未使用1');
        }
        $repData['charge_price'] = $chargePrice;
        
        
        return successResult($repData);
        
    }
    
}










